#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
#include <stdlib.h>
void SLInit(SL* psl)
{
	psl->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
	if (psl->a ==NULL)
	{
		perror("malloc failed!");
		return;
	}
	psl->capacity = 4;
	psl->size = 0;
}

void SLDestroy(SL* psl)
{
	free(psl->a);
	psl->a = NULL;
	psl->capacity = 0;
	psl->size = 0;
}

void SLCheckCapacity(SL* psl)
{
	if (psl->capacity == psl->size)
	{
		SLDatatype* tem = (SLDatatype*)realloc(psl->a, sizeof(SLDatatype) * psl->capacity * 2);
		if (psl == NULL) return;
		psl->a = tem;
		psl->capacity *= 2;
	}
}


void SLPrint(SL* psl)
{
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}

void SLPushBack(SL* psl, SLDatatype x)
{
	SLCheckCapacity(psl);

	psl->a[psl->size++] = x;
}

void SLPushFront(SL* psl, SLDatatype x)
{
	SLCheckCapacity(psl);
	int end = psl->size;
	while (end - 1 >= 0)
	{
		psl->a[end] = psl->a[end - 1];
		 end--;
	}
	psl->a[0] = x;
	psl->size++;
}

void SLPopBack(SL* psl)
{
	assert(psl->size > 0);
	//if (psl->size == 0)
	//	return;
	//if (psl->size >= 0)
		psl->size--;
}

void SLPopFront(SL* psl)
{
	assert(psl->size > 0);
	/*int start = 0;
	while (start + 1 <= psl->size - 1)
	{
		psl->a[start] = psl->a[start + 1];
		start++;
	}*/
	int start = 1;
	while (start <= psl->size - 1)
	{
		psl->a[start - 1] = psl->a[start];
		start++;
	}
	psl->size--;
}

void SLInsert(SL* psl, int pos, SLDatatype x)
{
	assert(pos >= 0 && pos <= psl->size);
	SLCheckCapacity(psl);

	int end = psl->size;
	while (end-1>=pos)
	{
		psl->a[end] = psl->a[end - 1];
		end--;
	}
	psl->a[pos] = x;
	psl->size--;
}

void SLErase(SL* psl, int pos)
{
	assert(pos >= 0 && pos < psl->size);

}

int SLFind(SL* psl, SLDatatype x)
{
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->a[i] == x)
			return i;
	}
	return -2;
}
